package com.example.lotterysystem.controller.handler;


import com.example.lotterysystem.common.errorcode.GlobalErrorCodeConstants;
import com.example.lotterysystem.common.exception.ControllerException;
import com.example.lotterysystem.common.exception.ServiceException;
import com.example.lotterysystem.common.pojo.CommonResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;



@RestControllerAdvice
public class GlobalException {

    private final static Logger logger = LoggerFactory.getLogger(GlobalException.class);

    @ExceptionHandler(value = ServiceException.class)
    public CommonResult<?> ServiceException(ServiceException e){
        logger.info("serviceException : " , e);
        return CommonResult.error(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode() ,
                                  e.getMessage());
    }

    @ExceptionHandler(value = ControllerException.class)
    public CommonResult<?> ControllerException(ControllerException e){
        logger.info("controllerException : " , e);
        return CommonResult.error(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode() ,
                e.getMessage());
    }

    @ExceptionHandler(value = Exception.class)
    public CommonResult<?> Exception(Exception e){
        logger.info("Exception : " , e);
        return CommonResult.error(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode() ,
                e.getMessage());
    }


}
